
    /*
     * There are many object production styles in javascript. One common 
     * implementation pattern is the definition of object literals, like
     * so
     */
    
    var CustomObject=
    {
        value: null
        
    ,   setValue: function (value) 
        {
            if (null == value)
                throw new Error ('No value to set');
            this.value= value;
        }
        
    ,   getValue: function () 
        {
            return this.value;
        }
    };
    
    /*
     * The problem is that there is only one instance of the object. What
     * if you wanted another? Often beginers think in terms of copying the object
     * to obtain another with the same functionality
     */
    
    function copy (source, destination) 
    {
        for (var accessor in source)
            destination[accessor]= source;
    };
    
    var CustomObjectCopy= copy (CustomObject, {});
    
    /*
     * But really what you want to do is define a class, and create instances
     * of that class. While JavaScript does not support classes, it is an object
     * based language and we can emulate them. This may be jarring, especially
     * if you are coming from a full fledged OOP background, but in JavaScript,
     * all functions are constructors. This means that sometimes you will see
     * implementation patterns based on defining a properties inside a constructor
     * like so.
     */
     
    function CustomObject (value)
    {
        this.value= value;
        this.getValue= function ()
        {
            return this.value;
        };
        this.setValue= function (value) 
        {
            if (null == value)
                throw new Error ('No value to set');
            this.value= value;
        }
    };
    
    var instance= new CustomObject('someValue')
    ,   value= instance.getValue(); // returns 'someValue'
    
    instance.setValue('otherValue'); // instance.value is now 'otherValue'
    
    /*
     * While this accomplishes the task, you should be wary of this approach,
     * as every time you instantiate the constructor, the properties are
     * allocated to the instance dynamically. 
     */
    
    var firstInstance= new CustomObject(1)
    ,   secondInstance=new CustomObject(2)
    ,   isSameMethod= firstInstance.getValue === secondInstance.getValue; // false
    
    /*
     * A much more efficient way to define a custom object is to add properties
     * to the constructors prototype and put only the instantiation logic in the 
     * constructor
     */
    
    function CustomObject (value) 
    {
        this.setValue(value);
    };
    
    CustomObject.prototype.value= null;
    CustomObject.prototype.getValue= function ()
    {
        return this.value;
    };
    CustomObject.prototype.setValue= function (value) 
    {
        if (null == value)
            throw new Error ('No value to set');        
        this.value= value;
    };
    
    var firstInstance= new CustomObject (0)
    ,   secondInstance=new CustomObject (1)
    ,   isSameMethod= firstInstance.getValue === secondInstance.getValue; // true
    
    /*
     * Defining objects like this can be verbose, so another pattern is to
     * take a reference to constructors prototype and use that instead, the
     * objective being to reduce the ammount of code that has to be typed
     */
    
    function CustomObject (value) 
    {
        this.setValue(value);
    };
    
    var C= CustomObject.prototype;
    C.value= null;
    C.getValue= function () 
    {
        return this.value;
    };
    C.setValue= function (value) 
    {
        if (null == value)
            throw new Error ('No value to set');          
        this.value= value;
    };
    
    /*
     * This leads to a cramped programming style which loses semantic value
     * and can become difficult to read, and so should be avoided. A much neater
     * way of doing it is to assign an object literal to the constructors
     * prototype, like so:
     */
    
    function CustomObject (value) 
    {
        this.setValue(value);
    };
    
    CustomObject.prototype= 
    {
        value: null
        
    ,   setValue: function (value) 
        {
            if (null == value)
                throw new Error ('No value to set');  
            this.value= value;
        }
        
    ,   getValue: function () 
        {
            return this.value;
        }
    };
    
    /*
     * As you can see, this is much tidier, and resembles the object literal
     * style that we set out with, but has the added benefit of defining an
     * object by defining its prototype. However, when we do this, we override
     * one special property already defined on a prototype, named 'constructor'
     */
    
    var instance= new Object
    ,   constructor= instance.constructor
    ,   isObject= constructor === Object // true
    ,   custom= new CustomObject(true)
    ,   isCustomObject= custom.constructor === CustomObject // false!
    
    /*
     * To avoid this confusing state of affairs, if we are using object literals
     * to define constructor prototypes always make sure to add the constructor
     * property yourself, like this
     */
    
    function CustomObject (value) 
    {
        this.setValue(value);
    };
    
    CustomObject.prototype= 
    {
        constructor: CustomObject // ensure the correct constructor!
    
    ,   value: null
        
    ,   setValue: function (value) 
        {
            if (null == value)
                throw new Error ('No value to set');  
        }
        
    ,   getValue: function () 
        {
            return this.value;
        }
    };
    
    /*
     * The above pattern should be considered a best practice for defining
     * custom objects natively, but it too has problems. What if you want to
     * extend a custom object? One common pattern is to use a variant of the
     * object copy pattern, often called extend or augment, but really its just 
     * a copy function with a guard clause against overwriting properties. Lets
     * call it augment, as thats more in line with the purpose of the function
     */
    
    function augment (subject, traits, preventOverride) 
    {
        for (var accessor in traits) 
        {
            if (preventOverride && accessor in subject)
                continue;
                
            subject[accessor]= traits;
        }
    };
    
    ///////////////////////////////////////
    
    function ExtendedCustomObject (value)
    {
        this.setValue(value);
    };

    ExtendedCustomObject.prototype= 
    {
        hasValue: function () 
        {
            return null !== this.getValue();
        }
    };

    // copy all of the properties of CustomObject prototype to ExtendedCustomObject
    // protecting against overwriting the constructor property
    augment(ExtendedCustomObject.prototype, CustomObject.prototype, true);    
    
    ///////////////////////////////////////

    
    /*
     * While this will effectively mean that an instance of ExtendedCustomOBject
     * has all of the same properties of CustomObject, it wont qualify as an
     * instance. Also, the use of external functions and having to patch the
     * constructor property after extending breaks the semantic value of the
     * class definition and adds noise.
     */
    
    var co= new CustomObject('1')
    ,   ex= new ExtendedCustomObject('2')
    ,   isCo= co instanceof CustomObject // true
    ,   isEx= ex instanceof ExtendedCustomObject // true
    ,   isCoInstance= ex instanceof CustomObject // false! in OOP, this should be true    
    
    /*
     * To achieve the result we're after in native javascript, we have to do 
     * something like this
     */
    
    /////////////////////////////////////////
    
    function ExtendedCustomObject (value) 
    {
        this.setValue(value);
    };
    
    // to 'extend' a 'class', you make the constructors protoype an instance of
    // another 'class'
    ExtendedCustomObject.prototype= new CustomObject;
    // but as we have assigned the prototype to an instance of CustomObject, 
    // we can no longer use object literals to define the prototype after
    // extending it, as we would end up re-assigning the constructors prototype
    // property! So we have either to go the long route, or use our copy utility    
    augment (ExtendedCustomObject.prototype, 
    {
        constructor: ExtendedCustomObject // you still need to reset the constructor property
    ,   hasValue: function () 
        {
            return null !== this.getValue();
        }
    });

    /////////////////////////////////////////

    /*
     * Apart from this being a noisy implementation, there is a problem. If you
     * look at the implementation of CustomObject, you will note that the 
     * instantiation logic in the CustomObject constructor is to call setValue, 
     * which in turn will throw an error if no value is provided, so by instantiating 
     * CustomObject to make it the prototype of ExtendedCustomObject, and not
     * supplying a value argument, an error will be thrown and the assignment will not be
     * made! Luckily, there is a work around, which we can encapsulate in a method
     */
    
    function extend (Child, Parent) 
    {
        // use an intermediary constructor with no instantiation logic
        function Intermediary () {;};
        
        // assign it the prototype of the constructor to inherit from
        Intermediary.prototype= Parent.prototype;
        
        // then instantiate the intermediary and make IT the prototype of
        // the constructor that will be extended
        Child.prototype= new Intermediary;
        Child.prototype.constructor= Child;
        Child.prototype.parent= Parent;
        
        return Child;
    };
    
    // declare our constructor
    function ExtendedCustomObject (value) 
    {
        this.setValue(value);
    };
    
    // extend it
    extend (ExtendedCustomObject, CustomObject); 
    
    // and lets use our augment method to copy the properties of an object
    // literal to the ExtendedCustomObject prototype
    augment (ExtendedCustomObject.prototype, 
    {
        hasValue: function () 
        {
            return null != this.value;
        }
    });
    
    /*
     * A little known feature of JavaScript is hoisting. Rather than explaining
     * it here, just understand that when you define a named function, its 
     * available to reference anywhere in the scope in which it is defined,
     * so that means you can change the order of your object production techniques
     * like so- 
     */
    
    extend (ExtendedCustomObject, CustomObject);
    augment (ExtendedCustomObject.prototype, 
    {
        hasValue: function () 
        {
            return null != this.value;
        }
    });
    
    // constructor at the bottom!
    function ExtendedCustomObject (value) 
    {
        this.setValue(value);
    };
    
    /*
     * However, the order of extend and augment must remain in that order for
     * the implementation to work- just remember that every time you extend,
     * you completely overwrite a constructors prototype and discard its 
     * previous prototype and all its properties. 
     * 
     * While all of our implementation requirements are now taken care of,
     * the implementation itself has low semantic value and is still relatively
     * noisey. What if we were to make the extend method available on all
     * constructors? We can't call it extends, as thats a reserved word, and its
     * confusing to use the work extend:
     * 
     *     MyClass.extend(YourClass); // which class is extended?
     * 
     * So lets call it inherits
     */
    
    Function.prototype.inherits= function (Parent) 
    {
        function Intermediary () {;};
        Intermediary.prototype= Parent.prototype;
        
        // 'this' refers to the constructor on which this method is called
        this.prototype= new Intermediary;
        this.prototype.constructor= this;
        this.prototype.parent= Parent;
        
        return this;
    };
    
    ///////////////////////////////////////////
    
    ExtendedCustomObject.inherit(CustomObject); // that reads well, right?
    augment(ExtendedCustomObject.prototype, // though this is still a little noisey
    {
        hasValue: function () 
        {
            return null != this.value;
        }
    });
    
    function ExtendedCustomObject (value)
    {
         this.setValue(value);
    }
    
    ///////////////////////////////////////////
    
    /*
     * So what can we do to reduce the noise of the augment method? To begin,
     * WHAT is it doing? Its implementing properties from one object on another.
     * The copy part of it is HOW its doing it, so what if there was an 
     * implement method available on a constructor?
     */
    
    Function.prototype.implement= function () 
    {
        for (var trait, i= 0, n= arguments.length; i<n; i++) 
        {
            trait= arguments[i];
            if (null == trait)
                continue;
            
            // copy all the properties if a supplied object to this prototype
            // remember that 'this' referes to the function on which the
            // implement method is called    
            for (var accessor in trait) 
            {
                this.prototype[accessor]= trait[accessor];
            }
        }
        
        return this;
    };
    
    
    ///////////////////////////////////////////
    
    ExtendedCustomObject.inherit(CustomObject); 
    ExtendedCustomObject.implement
    (
        {
            hasValue: function () 
            {
                return null != this.value;
            }
        }
    );
    
    function ExtendedCustomObject (value) 
    {
        this.setValue(value);
    };
    
    ///////////////////////////////////////////
    
    /*
     * Or, if we wanted to be more concise or space concious, we could take
     * advantage of the fact that both methods return a reference to the
     * constructor upon which they were invoked, and chain
     */
    
    
    ExtendedCustomObject.inherit(CustomObject).implement
    (
        {
            hasValue: function () 
            {
                return null != this.value;
            }
        }
    );
    
    function ExtendedCustomObject (value) 
    {
        this.setValue(value);
    };
    
    /*
     * Or simpler again, what if we wanted to avoid the object literal approach
     * and emulate a more classic object production style? The ObjectKit
     * implement method supports named functions, which means you can do this
     * too-
     */
    
    ExtendedCustomObject.inherit(CustomObject).implement
    (
        function hasValue () 
        {
            return null != this.value;
        }
    );
    
    function ExtendedCustomObject (value) 
    {
        this.setValue(value);
    };
    
    /* have fun! */
